package org.easy.tool.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.easy.tool.common.*;
import org.easy.tool.constant.TihaiConstant;
import org.easy.tool.entity.FileRecord;
import org.easy.tool.entity.Problem;
import org.easy.tool.mapper.FileRecordMapper;
import org.easy.tool.mapper.ProblemMapper;
import org.easy.tool.service.IFileRecord;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.easy.tool.service.IProblem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 文件服务
 *
 * @author Valley
 * @since 2024-09-08 13:27:04
 */
@Service
public class FileRecordService extends ServiceImpl<FileRecordMapper, FileRecord> implements IFileRecord {


    @Autowired
    ProblemMapper problemMapper;


    @Override
    public String upload(MultipartFile file) throws Exception {
        //记录上传
        FileRecord record = new FileRecord();
        String randomID = RandomUtil.randomID();
        record.setId(randomID);
        String fileName = file.getOriginalFilename();
        record.setFileName(fileName);
        record.setUserId("admin");
        String fileUrl = TihaiConstant.BASE_URL + "/fileRecord/" + randomID;
        record.setFileUrl(fileUrl);
        String[] split = fileName.split("\\.");
        record.setFileType(split[1]);
        record.setActionType("upload");
        record.setCreateTime(TimeUtil.now());
        record.setUpdateTime(TimeUtil.now());
        save(record);
        //上传文件
        String fileDir = TihaiConstant.FILE_DIR + File.separator + randomID;
        if (!new File(fileDir).exists()) {
            new File(fileDir).mkdirs();
        }
        String savePath = fileDir + File.separator + fileName;
        file.transferTo(new File(savePath));
        return randomID;
    }

    @Override
    public void preview(String id) throws Exception {
        String fileName = getById(id).getFileName();
        String filePath = TihaiConstant.FILE_DIR + File.separator + id + File.separator + fileName;
        HttpUtil.preview(WebUtil.req(), WebUtil.res(), filePath);
    }

    @Override
    public void exportWord(LinkedHashMap data) throws Exception {
        String docTitle = (String) data.get("docTitle");
        List<LinkedHashMap> list = (List<LinkedHashMap>) data.get("problemList");
        List<Problem> problemList = list.stream().map(item -> {
            LinkedHashMap content = (LinkedHashMap) item.get("content");
            String id = (String) content.get("id");
            return problemMapper.selectById(id);
        }).collect(Collectors.toList());
        String temp = TihaiConstant.FILE_TEMP_DIR + RandomUtil.randomID() + ".docx";
        List<String> contentList = new ArrayList<>();
        for (int i = 0; i < problemList.size(); i++) {
            Problem problem = problemList.get(i);
            String problemTitle = "第" + (i + 1) + "题:" + problem.getTitle();
            contentList.add(problemTitle);
            LinkedHashMap contentMap = TihaiConstant.MAPPER.readValue(problem.getContent(), LinkedHashMap.class);
            String contentImg = (String) contentMap.get("contentImg");
            String contentUrl = TihaiConstant.BASE_FILE_URL + contentImg;
            contentList.add(contentUrl);
//            String answerImg = (String) contentMap.get("answerImg");
//            String answerUrl = TihaiConstant.BASE_FILE_URL + answerImg;
//            contentList.add(answerUrl);
        }
        WordUtil.write07(temp, docTitle, contentList);
        HttpUtil.downloadFile(WebUtil.req(), WebUtil.res(), new File(temp));
    }
}
